package ewms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import ewms.bo.EwmsEntryWarehouseDetailAddBo;
import ewms.common.utils.PageUtils;
import ewms.common.core.page.PagePlus;
import ewms.common.core.page.TableDataInfo;
import ewms.service.IEwmsEntryWarehouseDetailService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import ewms.bo.EwmsEntryWarehouseDetailQueryBo;
import ewms.bo.EwmsEntryWarehouseDetailEditBo;
import ewms.domain.EwmsEntryWarehouseDetail;
import ewms.mapper.EwmsEntryWarehouseDetailMapper;
import ewms.vo.EwmsEntryWarehouseDetailVo;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 危废入库明细Service业务层处理
 *
 * @author zhaoxiaochun
 * @date 2022-02-09
 */
@Service
public class EwmsEntryWarehouseDetailServiceImpl extends ServiceImpl<EwmsEntryWarehouseDetailMapper, EwmsEntryWarehouseDetail> implements IEwmsEntryWarehouseDetailService {

    @Override
    public EwmsEntryWarehouseDetailVo queryById(Long entryWarehouseDetailId){
        return getVoById(entryWarehouseDetailId, EwmsEntryWarehouseDetailVo.class);
    }

    @Override
    public TableDataInfo<EwmsEntryWarehouseDetailVo> queryPageList(EwmsEntryWarehouseDetailQueryBo bo) {
        PagePlus<EwmsEntryWarehouseDetail, EwmsEntryWarehouseDetailVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), EwmsEntryWarehouseDetailVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<EwmsEntryWarehouseDetailVo> queryList(EwmsEntryWarehouseDetailQueryBo bo) {
        return listVo(buildQueryWrapper(bo), EwmsEntryWarehouseDetailVo.class);
    }

    private LambdaQueryWrapper<EwmsEntryWarehouseDetail> buildQueryWrapper(EwmsEntryWarehouseDetailQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<EwmsEntryWarehouseDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getEntryWarehouseId() != null, EwmsEntryWarehouseDetail::getEntryWarehouseId, bo.getEntryWarehouseId());
        lqw.eq(bo.getCollectDetailId() != null, EwmsEntryWarehouseDetail::getCollectDetailId, bo.getCollectDetailId());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(EwmsEntryWarehouseDetailAddBo bo) {
        EwmsEntryWarehouseDetail add = BeanUtil.toBean(bo, EwmsEntryWarehouseDetail.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(EwmsEntryWarehouseDetailEditBo bo) {
        EwmsEntryWarehouseDetail update = BeanUtil.toBean(bo, EwmsEntryWarehouseDetail.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(EwmsEntryWarehouseDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
